<?php

namespace App\Models\Order;

use Illuminate\Database\Eloquent\Model;

class Salesorder extends Model
{
    protected $table = 'sales_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        // $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with(['suborder' => function ($query) {
            $query->where('is_del', config('constants.is_del.no'));
        }])
            ->where('is_del', config('constants.is_del.no'))
            ->orderBy('create_at', 'desc');

        //单据日期
        $start_time = $request->start_time;
        if ($start_time != '') {
            $query->where('document_date', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $query->where('document_date', '<=', $end_time);
        }

        //客户名称
        $custom_name = $request->custom_name;
        if ($custom_name != '') {
            $query->where('custom_name', 'like', '%' . $custom_name . '%');
        }

        //单据编号
        $document_number = $request->document_number;
        if ($document_number != '') {
            $query->where('document_number', 'like', '%' . $document_number . '%');
        }

        $order_number = $request->order_number;
        if ($order_number != '') {
            $query->where('order_number', 'like', '%' . $order_number . '%');
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        // if ($sys_dpm_id > 0) {
        //     $query->where('sys_dpm_id', $sys_dpm_id);
        // }

        $baochedan_status = $request->baochedan_status ?? 0;
        if ($baochedan_status > 0) {
            $query->where('baochedan_status', $baochedan_status);
        }


        $produce_status = intval($request->produce_status);
        if ($produce_status > 0) {
            $produce_status=$produce_status==1?2:1;
            $query->where('produce_status', $produce_status);
        }

        /**
         * manger 1自己添加的数据，2所有数据
         * 销售只能看到自己数据,王丹娜说的 2025.03.27
        */
        $manger = $request->manger;
        if ($manger == 1 || $utype == '7') {
            $query->where('manger_id', '=', $manger_id);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    /**
     * 详情
     */
    public static function getone($id)
    {
        return self::with(['suborder' => function ($query) {
            $query->where('is_del', config('constants.is_del.no'));
        }])->where('id', $id)
            ->where('is_del', config('constants.is_del.no'))
            ->get();
    }

    /**
     * 获取子订单
     */
    public function suborder()
    {
        return $this->hasMany(Salesordersub::class, 'p_id', 'id');
    }
}
